<?php

namespace App\Business\BLL;

use App\Models\DayPoint;
use Carbon\Carbon;

class UserDaliyPointBLL
{
    public static function getUserPoint($userID, $point)
    {
        $limit = config('set.daily_limit');
        $now = Carbon::now()->toDateString();
        $dayPoint = DayPoint::firstOrCreate(
            ['user_id' => $userID, 'date' => $now]
        );
        $tmpPoint = $dayPoint->point + $point;
        $point = $tmpPoint <= $limit ? $point : $limit - $dayPoint->point;

        if ($point > 0) {
            $dayPoint->point += $point;
            $dayPoint->save();
        } else {
            $point = 0;
        }

        return $point;
    }

}
